import { AntdModalProps } from "@/hooks/useAntdModal"
import useQuery from "@/hooks/useQuery"
import { saveGateway } from "@/service/deliver"
import { Form, Input, Modal } from "antd"
import DataIdSelect from "./DataIdSelect"
import EnvSelect from "./EnvSelect"


const SaveGateway = ({ form, modalProps, confirmLoading, closeModal, refresh }: AntdModalProps) => {
  const query = useQuery()
  const onOk = async () => {
    let data = await form.validateFields()
    confirmLoading(true)
    await saveGateway(data)
    confirmLoading(false)

    refresh()
    closeModal()
  }
  return <Modal  {...modalProps} onOk={onOk}>
    <Form labelCol={{ span: 5 }} form={form} initialValues={{

    }}>
      <Form.Item hidden name='id' >
        <Input />
      </Form.Item>
      <Form.Item label='环境' name='env' rules={[{
        required: true,
        message: '环境'
      }]}>
        <EnvSelect />
      </Form.Item>
      <Form.Item name='name' label='名称' rules={[{
        required: true,
        message: '名称'
      }]} >
        <Input />
      </Form.Item>

      <Form.Item label="域名" name='host' rules={[{
        required: true,
        message: '域名'
      }]}>
        <Input />
      </Form.Item>
      <Form.Item label="路径" name='path' rules={[{
        required: true,
        message: '路径'
      }]}>
        <Input />
      </Form.Item>
      <Form.Item hidden name='group' />
      <Form.Item noStyle shouldUpdate={(a, b) => a.env !== b.env}>
        {({ getFieldValue }) => {
          const env = getFieldValue('env');
          
          if (env) {
            return <Form.Item name='resourceId' label='资源关联' rules={[{
              required: true,
              message: '资源关联'
            }]}>
              <DataIdSelect env={env} />
            </Form.Item>
          }
          return null
        }}
      </Form.Item>

      {/* <Form.Item label="group" name='group' rules={[{
        required: true,
        message: 'group'
      }]}>
        <Input disabled />
      </Form.Item>
      <Form.Item label="dataId" name='dataId' rules={[{
        required: true,
        message: 'dataId'
      }]}>
        <Input />
      </Form.Item> */}




    </Form>
  </Modal>
}
export default SaveGateway